<?php
/**
 * Object Relational Persistence Mapping Class for table: pha_ewh_exticket
 *
 * @package	modules.dept_pharmacy.ordo
 * @author	viethd
 * @version $
 * @copyright 2009 LHV Company
 */
class PhaEwhExticket extends ORDataObject {

	/**#@+
	 * Fields of table: pha_ewh_exticket mapped to class members
	 */
	var $pha_ewh_exticket_id		= '';
	var $exticket_code		= '';
	var $app_date		= '';
	var $app_user_id		= '';
	var $enum_status		= '';
	var $pha_req_exticket_id		= '';
	var $note = '';
	/**#@-*/


	/**
	 * DB Table
	 */
	var $_table = 'pha_ewh_exticket';

	/**
	 * Primary Key
	 */
	var $_key = 'pha_ewh_exticket_id';
	
	/**
	 * Internal Name
	 */
	var $_internalName = 'PhaEwhExticket';

	/**
	 * Handle instantiation
	 */
	function __construct() {
		parent::ORDataObject();
	}
	
    /**
     * generate export ticket code
     * [PN][YYYYMMDD + 3 digits]
     * @return [PN][YYYYMMDD + 3 digits]
     */
    public function generateEwhExpTicketCode() {
        $phaSequencesObj = Celini::newOrdo('PhaSequences');
        $seq = $phaSequencesObj->getExTicketSeq();
        //gen code
        $today = date("Ymd");
        $formatted = "PX" . $today . sprintf("%06s", $seq);
        return   $formatted;
    }

    public function createExTicket($data = array()) {
        $result = array('id'=>'', 'code' => '');
        $this->populateArray($data);
        $code = $this->generateEwhExpTicketCode();
        $this->exticket_code = $code;
        $this->persist();
        $result['id'] = $this->get_id();
        $result['code'] = $code;
        return  $result;
    }
    
    public function createRejectedExTicket($data = array()) {
        $result = array('id'=>'', 'code' => '');
        $this->populateArray($data);
        $this->persist();
        $result['id'] = $this->get_id();
        $result['code'] = '';
        return  $result;
    }

    public function setData($data = array()) {
        if (is_array($data)) {
            $this->populateArray($data);
        }
    }

    public function isExist($exTicketId) {
        $ds =& new Datasource_sql();
        $sql = array(
            'cols'  => "pha_ewh_exticket_id",
            'from'  => "$this->_table",
            'where' => "pha_ewh_exticket_id='$exTicketId'"
            
        );
        $ds->setup($this->_db, $sql);
        $result = $ds->toArray();
        if (empty($result)) {
            return false;
        }
        return true;
    }
    
    public function getExpTicketOfGivenReq($reqId) {

        $ds =& new Datasource_sql();
        $sql = array(
               'cols'  => "t.pha_ewh_exticket_id, t.exticket_code, t.app_user_id, pp.name app_user_name, t.app_date, t.enum_status, t.note",
               'from'  => "$this->_table t INNER JOIN $this->_table t1 ON (t.pha_ewh_exticket_id=t1.pha_ewh_exticket_id
                                                     AND t.pha_req_exticket_id='$reqId')                           
                           LEFT JOIN person pp on (t.app_user_id=pp.person_id) "
        );
        $ds->setup($this->_db, $sql);
        $result = $ds->toArray();
        if (empty($result)) {
            return null;
        }
        return $result[0];

    }
    
    public function rollBackPickDrugMaterial($reqId) {
        
    }
    
	
}
?>
